package com.ricchieri.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jasypt.util.password.ConfigurablePasswordEncryptor;
import org.springframework.transaction.annotation.Transactional;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.dao.interf.IUserDao;
import com.ricchieri.filter.EmailTemplateFilter;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.model.EmailTemplate;
import com.ricchieri.model.EmailToSend;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IEmailTemplateService;
import com.ricchieri.service.interf.IEmailToSendService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;

@Transactional(readOnly = true)
public class UserService implements IUserService, Serializable {
	private static final long serialVersionUID = 1586769044468426937L;
	
	IUserDao userDao;
	
	IEmailToSendService emailToSendService;
	IEmailTemplateService emailTemplateService;
	
	ConfigurablePasswordEncryptor configurablePasswordEncryptor;

	@Override
	@Transactional(readOnly = false)
	public void addUser(User user) {
		user.setPassword(configurablePasswordEncryptor.encryptPassword(user.getPassword()));
		user.setUsername(user.getDocumentType().getDescriptionKey().concat(user.getDni()));
		user.setTheme(BusinessSettings.DEFAULT_THEME);
		userDao.add(user);
		
		this.createWelcomeEmail(user);
	}
	
	
	private void createWelcomeEmail(User user) {
			
		EmailTemplateFilter emailTemplateFilter = new EmailTemplateFilter();
		emailTemplateFilter.setEmailTemplateTypeDescription(BusinessSettings.EMAIL_TEMPLATE_WELCOME);
		EmailTemplate emailTemplate = emailTemplateService.getEmailTemplateByFilter(emailTemplateFilter);
		
		if(emailTemplate != null && user.getDefaultEmail() != null) {
			EmailToSend emailToSend = new EmailToSend();
			
			String content = emailTemplate.getTemplate();
			String fullnameTag = FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME);
			String usernameTag = FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_USERNAME);

			content = content.replace("{".concat(fullnameTag).concat("}"), user.getFullName());
			content = content.replace("{".concat(usernameTag).concat("}"), user.getUsername());
			
			emailToSend.setUser(user);
			emailToSend.setMessageContent(content);
			emailToSend.setEmailToSendType(emailTemplate.getEmailToSendType());
			emailToSend.setSubjectContent(emailTemplate.getSubject());
			
			emailToSendService.add(emailToSend);
		}
	}
	
	@Override
	@Transactional(readOnly = false)
	public void unauthorizeUser(User user) {
		user.setAutorized(Boolean.FALSE);
		userDao.update(user);
	}
	
	@Override
	@Transactional(readOnly = false)
	public void authorizeUser(User user) {
		user.setAutorized(Boolean.TRUE);
		userDao.update(user);
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	@Transactional(readOnly = true)
	public List<User> getAllUserByFilter(UserFilter user) {
		return userDao.getAllUserByFilter(user);
	}

	@Override
	@Transactional(readOnly = true)
	public List<User> getAllUsers() {
		UserFilter filter = new UserFilter();
		return userDao.getAllUsers(filter);
	}

	@Override
	@Transactional(readOnly = true)
	public List<User> getAllUserPaginatedByFilter(UserFilter user, int page) {
		return userDao.getAllUserPaginatedByFilter(user, page);
	}

	@Override
	@Transactional(readOnly = true)
	public Integer getAllUserCountByFilter(UserFilter user) {
		return userDao.getAllUserCountByFilter(user);
	}

	@Override
	@Transactional(readOnly = true)
	public User getUserByUsername(String userName) {
		UserFilter filter = new UserFilter();
		filter.setUsername(userName);
		return userDao.getUserById(filter);
	}

	public ConfigurablePasswordEncryptor getConfigurablePasswordEncryptor() {
		return configurablePasswordEncryptor;
	}

	public void setConfigurablePasswordEncryptor(
			ConfigurablePasswordEncryptor configurablePasswordEncryptor) {
		this.configurablePasswordEncryptor = configurablePasswordEncryptor;
	}


	@Override
	@Transactional(readOnly = false)
	public void deleteUser(User selectedUser) {
		selectedUser.setActive(Boolean.FALSE);
		userDao.update(selectedUser);
	}


	@Override
	@Transactional(readOnly = false)
	public void updateUser(User user) {
		user.setUsername(user.getDocumentType().getDescriptionKey().concat(user.getDni()));
		userDao.update(user);
	}
	
	@Override
	public User getUserByFilter(UserFilter user){
		return userDao.getUserByFilter(user);
	}
	
	@Override
	public User getUserById(UserFilter userFilter){
		return userDao.getUserById(userFilter);
	}
	
	@Override
	public Map<String, String> getComboUserByRole(UserFilter userFilter){
		List<User> user = this.getAllUserByFilter(userFilter);

		HashMap<String, String> users = new HashMap<String, String>();

		for (User userT : user) {
			users.put(userT.getFullName(), String.valueOf(userT.getId()));
		}
		
		return users;
	}

	public IEmailToSendService getEmailToSendService() {
		return emailToSendService;
	}

	public void setEmailToSendService(IEmailToSendService emailToSendService) {
		this.emailToSendService = emailToSendService;
	}

	public IEmailTemplateService getEmailTemplateService() {
		return emailTemplateService;
	}
	
	public void setEmailTemplateService(IEmailTemplateService emailTemplateService) {
		this.emailTemplateService = emailTemplateService;
	}
}
